<?php
/**
 * Aero - Xistins Technologies PHP Framework
 * 
 * LICENSE
 * This source file is subject to the New BSD license that is bundled 
 * with this package in the LICENSE.txt. It is also available through 
 * the world-wide-web at this URL: http://dev.xistins.com/license/production
 * If you did not receive a copy of this license and are unable to obtain
 * it through the world-wide-web, please send an email to 
 * license@xistins.com so we can send you a copy immediately.
 * 
 * @category   Aero
 * @package    Aero_Loader
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Loader.php 4 2009-11-14 18:35:45Z csouth@xistins.com $
 */

/**
 * Class autoloader
 * 
 * @category   Aero
 * @package    Aero_Loader
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Loader.php 4 2009-11-14 18:35:45Z csouth@xistins.com $
 */
class Aero_Loader
{
    /**
     * Turn autoloader on or off
     * 
     * @param  bool $on
     * @return void
     */
    public static function autoload($on = true)
    {
        if($on) {
            spl_autoload_register(array('self', 'loadClass'));
        } else {
            spl_autoload_unregister(array('self', 'loadClass'));
        }
    }

    /**
     * Load class
     * 
     * @param  string $class
     * @return bool True if class is loaded False if not
     */
    public static function loadClass($class)
    {
        if(class_exists($class)) {
            return true;
        }

        $includePaths = explode(PATH_SEPARATOR, get_include_path());

        //Aero Type naming schema
        $classFile = str_replace('_', '/', $class) . '.php';
        foreach($includePaths as $path) {
            if(file_exists($path.'/'.$classFile)) {
                require_once $classFile;
                return TRUE;
            }
        }

        // ClassName.class.php
        // I don't know why but some people still use this
        $classFile = $class.'.class.php';
        foreach($includePaths as $path) {
            if(file_exists($path.'/'.$classFile)) {
                require_once $classFile;
                return TRUE;
            }
        }

        return FALSE;
    }
}